<?php
    session_start();    

    if(!isset($_SESSION['username']))
    {
        header('Location: index.php');
    }
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Files</title>
    </head>
    <body>
        <?php
            echo '<div>Current user: "'.$_SESSION['username'].'"</div>';
            echo '<a href="logout.php">Log out</a>';
        ?>
        <table border="1">
            <?php
                if(!file_exists($_SESSION['username'].DIRECTORY_SEPARATOR))
                {
                    echo '<tr>';
                    echo '<td>No uploaded files</td>';
                    echo '<td>---------</td>';
                    echo '</tr>';
                }
                else
                {
                    $listOfFiles = scandir($_SESSION['username'].DIRECTORY_SEPARATOR);
                    $counter = count($listOfFiles);

                    echo '<tr>';
                    echo '<td>File name</td>';
                    echo '<td>Size</td>';
                    echo '</tr>';
                    for($i = 2; $i < $counter; $i++)
                    {
                        $size = filesize($_SESSION['username'].DIRECTORY_SEPARATOR.$listOfFiles[$i]);
                        echo '<tr>';
                        echo '<td><a href="'.$_SESSION['username'].DIRECTORY_SEPARATOR.$listOfFiles[$i].'">'.$listOfFiles[$i].'</a></td>';
                        echo '<td>'.$size.' bytes</td>';
                        echo '</tr>';
                    }
                }
            ?>
            
        </table>
        <div>
            <a href="uploadForm.php">Upload a new file</a>
        </div>
    </body>
</html>
